package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.Banner;
import com.rockcent.mall.lib.domain.Topic;
import com.rockcent.mall.lib.domain.enumclass.ProductType;
import com.rockcent.mall.lib.dto.ProductDto;
import com.rockcent.mall.lib.dto.TopicDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;

/**
 * Created by zhangjh on 2016/11/15.
 */
@Repository
public class TopicRepositoryImpl implements TopicRepositoryCustom {
    private static final Logger log = LoggerFactory.getLogger(TopicRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public PageResult<TopicDto> findTopicQuery(Long mallId, String title, String beginDate, String endDate, Whether whether, PageResult pageResult) {

        SQLPageDoing<TopicDto> doing = (sql, fields, params, typeReferenceMap) -> {

            sql.append("SELECT %s FROM topic p WHERE 1=1 and is_delete='NO' ");
            fields.append("p.*");

            //主题标题
            if (StringUtils.isNotBlank(title)) {
                sql.append(" AND p.title LIKE :title");
                params.put("title", "%" + title + "%");
            }
            //主题标题
            if (null != mallId) {
                sql.append(" AND p.mall_id LIKE :mallId");
                params.put("mallId", mallId);
            }

            //创建开始时间
            if (StringUtils.isNotBlank(beginDate)) {
                sql.append(" AND p.date_created >= :beginDate");
                params.put("beginDate", beginDate);
            }
            //创建结束时间
            if (StringUtils.isNotBlank(endDate)) {
                sql.append(" AND p.date_created <= :endDate");
                params.put("endDate", endDate);
            }
            sql.append(" order by seq_no asc,date_created desc ");
        };
        return doing.go(pageResult, TopicDto.class, objectMapper, jdbcTemplate, log);
    }
    @Override
    public PageResult<TopicDto> findTopicPage(Long mallId,Long id,PageResult pageResult){
        SQLPageDoing<TopicDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM topic tp WHERE 1=1 and tp.is_delete='NO' and tp.mall_id = :mallId ");
            if (id != null){
                sql.append(" and tp.id = :id ");
                params.put("id",id);
            }
            sql.append(" order by tp.seq_no asc,tp.date_created desc ");
            fields.append("tp.*");
            params.put("mallId",mallId);
        };
        return doing.go(pageResult, TopicDto.class, objectMapper, jdbcTemplate, log);
    }
}
